package com.ys.web.wallet;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ys.entity.WalletAddress;
import com.ys.entity.vo.Withdraw;
import com.ys.enu.ResultCodeEnum;
import com.ys.exception.base.ParamException;
import com.ys.service.WalletAddressServiceImap;
import com.ys.service.wallet.WalletServicceContext;
import com.ys.service.wallet.base.IWalletService;
import com.ys.web.base.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 钱包接口
 */
@RestController
@Slf4j
@Api("创建钱包")
public class WalletController {

    @Autowired
    private WalletServicceContext walletServicceContext;

    /**
     * 创建钱包地址
     * @param currency
     * @return
     */
    @PostMapping("/createAddress")
    @ApiOperation("创建钱包地址")
    public ResultBody createAddress(Integer currency) throws Exception {
        if(currency==null){
            log.warn("创建钱包地址，资产类型不正确:currency="+currency);
            throw new ParamException("资产类型不正确",ResultCodeEnum.illegalargument);
        }
        IWalletService walletService=walletServicceContext.getWalletRPC(currency);
        return walletService.createAddress();

    }



    @PostMapping("/validateAddress")
    @ApiOperation("验证钱包地址")
    public ResultBody validateAddress(String address,Integer currency) throws Exception {
        if(currency==null){
            log.warn("创建钱包地址，资产类型不正确:currency="+currency);
            throw new ParamException("资产类型不正确",ResultCodeEnum.illegalargument);
        }

        if(StrUtil.isBlank(address)){
            log.warn("钱包地址不能为空:address="+address);
            throw new ParamException("资产类型不正确",ResultCodeEnum.illegalargument);
        }
        IWalletService walletService=walletServicceContext.getWalletRPC(currency);
        return walletService.validateAddress(address);
    }

    @PostMapping("/balanceByAddress")
    @ApiOperation("查询钱包余额")
    public ResultBody balanceByAddress(String address,Integer currency) throws Exception {
        if(currency==null){
            log.warn("创建钱包地址，资产类型不正确:currency="+currency);
            throw new ParamException("资产类型不正确",ResultCodeEnum.illegalargument);
        }

        if(StrUtil.isBlank(address)){
            log.warn("钱包地址不能为空:address="+address);
            throw new ParamException("钱包地址错误！",ResultCodeEnum.illegalargument);
        }
        IWalletService walletService=walletServicceContext.getWalletRPC(currency);
        return walletService.balanceByAddress(address);
    }


    /**
     * 提币
     * @param withdraw
     * @return
     */
    @PostMapping("/withdraw")
    @ApiOperation("提币")
    public ResultBody withdraw(Withdraw withdraw) throws Exception {

        if(withdraw==null){
            log.warn("提币非法参数:withdraw is null");
            throw new ParamException("withdraw is null",ResultCodeEnum.illegalargument);
        }

        if(StrUtil.isBlank(withdraw.getAddress()) || StrUtil.isBlank(withdraw.getSysNum())
                || withdraw.getAmount()<=0 || withdraw.getCurrency()==null
                || (withdraw.getFee()!=null && withdraw.getFee()<=0)
                ){
            log.warn("提币非法参数:withdraw="+ JSONUtil.toJsonStr(withdraw));
            throw new ParamException("withdraw is null",ResultCodeEnum.illegalargument);
        }

        IWalletService walletService=walletServicceContext.getWalletRPC(withdraw.getCurrency());

        return walletService.withDraw(withdraw);

    }


}